Ext.ns('Wlanauth.system');

Wlanauth.system.ConfigPanel = Ext.extend(Wlanauth.BaseFormPanel,{
	title:'系统配置',
	
	constructor:function(config){
		Ext.applyIf(this,config);
	 	
		Wlanauth.system.ConfigPanel.superclass.constructor.call(this,{
			labelWidth:80,
			padding:5,
			autoScroll:true,
			items:[{
//				fieldLabel:'计费比率',
//				xtype: 'compositefield',
//                combineErrors: false,
//                items: [{
//                	xtype: 'displayfield',width:40,value:'1',cls:'x-field-display'
//                },{
//                	xtype: 'displayfield',value: 'KB / ',width:40
//                },{
//                	name : 'properties[0].value',xtype: 'numberfield',
//                	key:'RADIUS_RATIO',width: 100,allowBlank: false
//                },{
//                	xtype: 'displayfield',value: '元',width:90
//                },{
//                	xtype: 'button',text:'保存',handler:this.saveSingle
//                },{
//                	xtype:'hidden',name:'properties[0].key',value:'RADIUS_RATIO'
//                }]
//			},{
				fieldLabel:'流量计算规则',
				xtype: 'compositefield',
                combineErrors: false,
                items: [{
                	xtype: 'displayfield',width:40,value:'1',cls:'x-field-display'
                },{
                	xtype: 'displayfield',value: ' : ',width:40
                },{
                	name : 'properties[1].value',xtype: 'numberfield',
                	key:'OCTRULE_RATIO',width: 100,allowBlank: false
                },{
                	xtype:'displayfield',width:90
                },{
                	xtype: 'button',text:'保存',handler:this.saveSingle
                },{
                	xtype:'hidden',name:'properties[1].key',value:'OCTRULE_RATIO'
                }]
			}],
			tbar:['点击此处',{
				text:'保存全部',
				iconCls:'edit',
				//handler:this.saveAll,
				scope:this
			}]
			
		});
		this.initData();
	},
	initData:function(){
		Ext.Ajax.request({
			url:'systemProperty!getAllProperties.action',
			scope:this,
			success:function(response,options){
				var obj = Ext.decode(response.responseText);
				if(obj.success){
					var properties = obj.rows;
					if(properties!=null){
						this.setPropertiesToForm(properties,this.getForm());
					}
				} else {
					Ext.Msg.alert('初始数据失败',obj.data||'未知原因');
				}
			
			}
		});
	},
	saveSingle:function(){
		var field = this.ownerCt.items.itemAt(2);
		
		var p = {
			'systemProperty.key':field.key,
			'systemProperty.value':field.getValue()
		}
		Ext.Ajax.request({
			url:'systemProperty!save.action',
			params:p,
			success:function(response,opts){
				var obj = Ext.decode(response.responseText);
				if(obj.success){
					Ext.Msg.alert('提示','保存成功');
				} else {
					Ext.Msg.alert('初始数据失败',obj.data||'未知原因');
				}
			}
		})
	},
	saveAll:function(){
		var _form = this.getForm();
      	if(this.isValid(_form)){
      		_form.submit({
      			url:'systemProperty!saveProperties.action',
      			scope:this,
      			waitMsg:'正在保存数据...',
      			success:function(form,action){
      				Ext.Msg.alert('提示','所有配置已成功保存');
      			}
      		});
      	}
	},
	setPropertiesToForm:function(properties,form){
		var values = form.getValues();
		for(var attr in values){
			var field = form.findField(attr);
			for(var i=0;i<properties.length;i++){
				var p = properties[i];
				if(p.key==field.key){
					field.setValue(p.value);
					field.propertyId = p.id;
					break;
				}
			}	
		}
	}
});

Ext.reg('wlanauth.system.configpanel',Wlanauth.system.ConfigPanel);